//
//  LDTLoginController.m
//  EPA
//
//  Created by lanou3g on 16/4/25.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "LDTLoginController.h"
#import "LDTNetWorkRequestManager.h"
#import "UserInfoManager.h"
#import "LDTRegistController.h"

@interface LDTLoginController ()

///邮箱
@property (weak, nonatomic) IBOutlet UITextField *emailTextFiled;
///密码
@property (weak, nonatomic) IBOutlet UITextField *passWordTextFiled;

@end

@implementation LDTLoginController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;


}

#pragma mark - 登陆按钮

- (IBAction)didClickLoginButn:(UIButton *)sender
{
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:LOGIN_URL] Method:LDT_POST Params:@{@"email":self.emailTextFiled.text, @"passwd":self.passWordTextFiled.text} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString *msg = jsonDict[@"data"][@"msg"];
        msg = msg ? msg :@"登陆成功";
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:(UIAlertControllerStyleAlert)];
        
        [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            if ([jsonDict[@"result"] isEqual:@1]) {
  
                [self dismissViewControllerAnimated:YES completion:nil];
                
                [[NSNotificationCenter defaultCenter]postNotificationName:@"login" object:nil];

            }
            
        }]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:alertVC animated:YES completion:nil];
        });
        
        if ([jsonDict[@"result"] isEqual:@1]) {
            [[UserInfoManager shareManager]loginWithUserDict:jsonDict[@"data"]];
            
        }
        
    }];
    
}

#pragma mark - 注册按钮

- (IBAction)didClickRegistBtn:(UIButton *)sender
{
    LDTRegistController *registVC = [[LDTRegistController alloc]init];
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:registVC];
    [self presentViewController:naVC animated:YES completion:nil];
}


#pragma mark - 返回按钮

- (void)leftButtonAction
{
     [self dismissViewControllerAnimated:YES completion:nil];
}





//UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
//
//snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//    
//    //          获取微博用户名、uid、token等
//    
//    if (response.responseCode == UMSResponseCodeSuccess) {
//        
//        UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
//        
//    }});








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
